<?php

use think\facade\Env;

function show($code = 200, $msg = "", $data = [], $httpStatus = 200)
{
    //如果消息提示为空，并且业务状态码定义了，那么就显示默认定义的消息提示
    if (empty($msg) && !empty(config("status") [$code])) {
        $msg = config("status") [$code];
    }

    $result = [
        'code' => $code,
        'msg' => $msg,
        'data' => $data
    ];

    return json($result, $httpStatus);
}


/**
 * 分类处理
 */
function listToTree($array, $id, $pid)
{
    //第一步 构造数据
    $items = array();
    foreach ($array as $value) {
        $items[$value[$id]] = $value;
    }
    //第二步 遍历数据 生成树状结构
    $tree = array();
    foreach ($items as $key => $value) {
        if (isset($items[$value[$pid]])) {
            $items[$value[$pid]]['children'][] = &$items[$key];
        } else {
            if (!isset($items[$key]['children'])) {
                $items[$key]['children'] = [];
            }
            $tree[] = &$items[$key];
        }
    }
    unset($item);
    return $tree;
}

function array_group_by($arr, $key)
{
    $grouped = [];
    foreach ($arr as $value) {
        $grouped[$value[$key]][] = $value;
    }
    if (func_num_args() > 2) {
        $args = func_get_args();
        foreach ($grouped as $key => $value) {
            $parms = array_merge([$value], array_slice($args, 2, func_num_args()));
            $grouped[$key] = call_user_func_array('array_group_by', $parms);
        }
    }
    return $grouped;
}